/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.enrichedassoudt;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen.assoUdt.AssoUdtEnrichedInfoGenerator;
import java.util.ArrayList;
import java.util.Arrays;

import com.inspur.edp.cef.generator.overall.Utils;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class EnrichedGetSetValueGenerator extends ClassPropertyGenerator {
  private IGspCommonField field;
  private boolean convertToUpper = false;
  public EnrichedGetSetValueGenerator(IGspCommonField field) {
    this.field = field;
  }

  public EnrichedGetSetValueGenerator(IGspCommonField field, boolean convertToUpper) {
    this.field = field;
    this.convertToUpper = convertToUpper;
  }

  @Override
  protected boolean isGetterOverride() {
    return true;
  }

  @Override
  protected boolean isSetterOverride() {
    return true;
  }

  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    AnnotationInfo annotationInfo = new AnnotationInfo();
    annotationInfo.setAnnotationType(AnnotationType.SingleMember);
    annotationInfo.setTypeInfo(new TypeInfo(JsonIgnore.class));
    annotationInfo.setSingleParam(ast.newBooleanLiteral(true));

    return new java.util.ArrayList<>(
        java.util.Arrays.asList(annotationInfo));
  }

  @Override
  protected Block buildGetMethodBody() {
    MethodInvocation invocation = ast.newMethodInvocation();
    if(convertToUpper){
      invocation.setName(ast.newSimpleName("get"+Utils.toUpperCaseFirstOne(field.getLabelID())));
    }
    else {
      invocation.setName(ast.newSimpleName("get"+field.getLabelID()));
    }
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(invocation);
    Block block = ast.newBlock();
    block.statements().add(returnStatement);
    return block;
  }

  @Override
  protected Block buildSetMethodBody() {
    MethodInvocation invocation = ast.newMethodInvocation();
    if(convertToUpper){
      invocation.setName(ast.newSimpleName("set"+Utils.toUpperCaseFirstOne(field.getLabelID())));
    }
    else {
      invocation.setName(ast.newSimpleName("set"+field.getLabelID()));
    }
    invocation.arguments().add(ast.newName("value"));
    Block block = ast.newBlock();
    block.statements().add(ast.newExpressionStatement(invocation));
    return block;
  }

  @Override
  protected ArrayList<ModifierKeyword> getGetterAccessModifier() {
    return new java.util.ArrayList<ModifierKeyword>(
        java.util.Arrays.asList(new ModifierKeyword[] {ModifierKeyword.PUBLIC_KEYWORD}));
  }

  @Override
  protected ArrayList<ModifierKeyword> getSetterAccessModifier() {
    return new java.util.ArrayList<ModifierKeyword>(
        java.util.Arrays.asList(new ModifierKeyword[] {ModifierKeyword.PUBLIC_KEYWORD}));
  }

  @Override
  protected TypeInfo getPropertyType() {
    return AssoUdtEnrichedInfoGenerator.getItemTypeInfo(field);
  }

  @Override
  protected String getPropertyName() {
    return "Value";
  }
}
